package com.open4cn.simulator.connect.com.listenner;

import com.open4cn.simulator.core.connector.ConnectAddress;
import com.open4cn.simulator.core.connector.IConnector;
import com.open4cn.simulator.core.connector.serialport.SerialPortChannel;
import com.open4cn.simulator.core.connector.serialport.SerialPortOpenListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author yinxiaopeng
 * @version 1.0.0
 * @company 致景科技
 * @department 飞梭BU/研发
 * @description
 * @Email yinxiaopeng@zj.tech
 * @date 2022-06-11 23:53
 */
public class ReconnectListener implements SerialPortOpenListener {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private IConnector connector;

    private ConnectAddress address;

    public ReconnectListener(IConnector connector, ConnectAddress address) {
        this.connector = connector;
        this.address = address;
    }

    @Override
    public void operationComplete(SerialPortChannel serialPortChannel) {
        if (null == serialPortChannel) {
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    connector.connect(address);
                }
            }, 1000L);
        }
    }
}
